﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace ryan
{
    class ScreenBoot
    {
        private const float FREQUENCY_MOVEMENT = 80f;
        private const float WAIT_RYAN = 2000f;
        private const float WAIT_JAMK = 0f;
        private const int IMPULSION = 17;

        private float freqMove, scale, waitRyan, waitJAMK;
        private Texture2D spriteTextureBackground, spriteTextureBackground2, spriteTextureMoon, spriteTextureRyan, spriteTextureJAMK, moonCircle;
        private Rectangle backgroundBox, backgroundBox2, moonBox, ryanBox, jamkBox;
        private int ryanFrameColone, ryanFrameLigne, impulsion, ground;
        private SpriteEffects effetMirroir;
        private Color colorBackground;
        private GraphicsDevice GraphicsDevice;
        private SpriteFont fontImpact;
        private Vector2 circlePosition;
        private bool statut;
        private Text gameText, presentText;

        public ScreenBoot(float newScale, GraphicsDevice GraphicsDevice)
        {
            Circle circle;

            this.ryanFrameColone = 1;
            this.ryanFrameLigne = 1;
            this.effetMirroir = SpriteEffects.None;
            this.freqMove = FREQUENCY_MOVEMENT;
            this.impulsion = IMPULSION;
            this.ground = 369;
            this.waitRyan = WAIT_RYAN;
            this.waitJAMK = WAIT_JAMK;
            this.colorBackground = Color.FromNonPremultiplied(255, 255, 255, 255);
            this.scale = newScale;
            this.backgroundBox = new Rectangle(0, 0, (int)(1920 / 2.4), (int)(1080 / 2.25));
            this.backgroundBox2 = new Rectangle(0, 0, (int)(1920 / 2.4), (int)(1080 / 2.25));
            this.jamkBox = new Rectangle(285, 220, (int)(566 / 2.4), (int)(386 / 2.25));
            this.moonBox = new Rectangle(287, 42, (int)(535 / 2.3), (int)(531 / 2.3));
            this.ryanBox = new Rectangle(0, 369, 60, 60);
            this.GraphicsDevice = GraphicsDevice;

            circle = new Circle(this.GraphicsDevice, 65, Color.White);
            circle.SetFill(true);
            this.moonCircle = circle.GetCircle();

            this.gameText = new Text("GAMES", Color.Black, Color.White, 1, 2, 0, new Vector2(580, 345));
            this.presentText = new Text("PRESENTE", Color.Black, Color.White, 1, 2, 0, new Vector2(405, 800));

            this.circlePosition = new Vector2(335, 92);
            this.statut = true;
        }

        public void LoadContent(ContentManager theContentManager)
        {
            this.spriteTextureBackground = theContentManager.Load<Texture2D>("BootScreen/BootScreen");
            this.spriteTextureBackground2 = theContentManager.Load<Texture2D>("BootScreen/BootScreen2");
            this.spriteTextureJAMK = theContentManager.Load<Texture2D>("BootScreen/BootScreenJAMK");
            this.spriteTextureMoon = theContentManager.Load<Texture2D>("BootScreen/BootScreenMoon");
            this.spriteTextureRyan = theContentManager.Load<Texture2D>("sprite");
            this.fontImpact = theContentManager.Load<SpriteFont>("SpriteFont_Impact");
        }

        public bool GetStatut()
        {
            return this.statut;
        }

        public void RyanRun(GameTime gameTime)
        {
            this.ryanFrameLigne = 1;
            this.freqMove -= gameTime.ElapsedGameTime.Milliseconds;

            if (this.ryanFrameColone != 2 && this.ryanFrameColone != 3)
            {
                this.ryanFrameColone = 2;
            }

            if (this.freqMove < 0f)
            {
                if (this.ryanFrameColone == 3)
                {
                    this.ryanFrameColone = 2;
                }
                else if (this.ryanFrameColone == 2)
                {
                    this.ryanFrameColone = 3;
                }

                this.freqMove = FREQUENCY_MOVEMENT;
            }
        }

        public bool RyanJump(int groundFinish)
        {
            if (this.impulsion < 0)
            {
                this.ground = groundFinish;
            }

            if (this.ryanBox.Y <= this.ground && this.ryanBox.Y != groundFinish)
            {
                this.ryanFrameColone = 2;
                this.ryanFrameLigne = 3;
                this.impulsion -= 1;
                this.ryanBox.Y -= this.impulsion;
                return true;
            }
            else
            {
                this.ryanBox.Y = this.ground;
                this.impulsion = IMPULSION;
                return false;
            }
        }

        private void DrawText(SpriteBatch sb,SpriteFont font, string text, Color backColor, Color frontColor, float scale,int borderSize, float rotation, Vector2 position)
        {
            //If we want to draw the text from the origin we need to find that point, otherwise you can set all origins to Vector2.Zero.
            Vector2 origin = new Vector2(font.MeasureString(text).X / 2, font.MeasureString(text).Y / 2);

            //These 4 draws are the background of the text and each of them have a certain displacement each way.
            sb.DrawString(font, text, position + new Vector2(borderSize * scale, borderSize * scale), backColor, rotation, origin, scale, SpriteEffects.None, 1f);
            sb.DrawString(font, text, position + new Vector2(-borderSize * scale, -borderSize * scale), backColor, rotation, origin, scale, SpriteEffects.None, 1f);
            sb.DrawString(font, text, position + new Vector2(-borderSize * scale, borderSize * scale), backColor, rotation, origin, scale, SpriteEffects.None, 1f);
            sb.DrawString(font, text, position + new Vector2(borderSize * scale, -borderSize * scale), backColor, rotation, origin, scale, SpriteEffects.None, 1f);

            //This is the top layer which we draw in the middle so it covers all the other texts except that displacement.
            sb.DrawString(font, text, position,frontColor, rotation, origin,scale,SpriteEffects.None, 1f);
        }

        public void Update(MouseState mouse, KeyboardState keyboard, GamePadState gamePad, GameTime gameTime)
        {
            int vitesse = 5;
            bool ryanIsJumping;
            this.ryanBox.X += 2;

            if (keyboard.IsKeyDown(Keys.Enter))
            {
                this.statut = false;
            }

            if (this.waitRyan <= 0f && this.ryanBox.X < 380)
            {
                this.ryanBox.X -= 2;
                if (this.colorBackground.R > 0 && this.colorBackground.G > 0 && this.colorBackground.B > 0 && this.colorBackground.A > 0)
                {
                    this.colorBackground.A -= 3;
                    this.colorBackground.R -= 3;
                    this.colorBackground.G -= 3;
                    this.colorBackground.B -= 3;
                }
                else
                {

                    if (this.waitJAMK > 3000f)
                    {

                        this.jamkBox.Y -= vitesse;
                        this.ryanBox.Y -= vitesse;
                        this.circlePosition.Y -= vitesse;
                        this.gameText.Position.Y -= vitesse;

                        if (this.presentText.Position.Y > 250)
                        {
                            this.presentText.Position.Y -= vitesse;
                        }
                        else
                        {
                            this.statut = false;
                        }
                    }
                    else
                    {
                        this.waitJAMK += gameTime.ElapsedGameTime.Milliseconds;
                    }
                }
            }
            else if (this.waitRyan <= 0f && this.ryanBox.X < 400)
            {
                this.ryanBox.X -= 4;
                ryanIsJumping = RyanJump(230);
            }
            else if (this.waitRyan <= 0f)
            {
                this.ryanBox.X -= 4;
                RyanRun(gameTime);
                this.waitRyan = 0f;
            }
            else
            {
                if (this.ryanBox.X > 445)
                {
                    this.ryanFrameColone = 1;
                    this.ryanFrameLigne = 1;
                    this.ryanBox.X -= 2;
                    this.effetMirroir = SpriteEffects.FlipHorizontally;
                    this.waitRyan -= gameTime.ElapsedGameTime.Milliseconds;
                }
                else if (this.ryanBox.X > 310)
                {
                    ryanIsJumping = RyanJump(245);
                    if (ryanIsJumping == false)
                    {
                        RyanRun(gameTime);
                    }
                }
                else if (this.ryanBox.X < 170)
                {
                    RyanRun(gameTime);

                }
                else
                {
                    ryanIsJumping = RyanJump(302);
                    if (ryanIsJumping == false)
                    {
                        RyanRun(gameTime);
                    }
                }
            }

        }

        public void Draw(SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.Draw(this.spriteTextureBackground, this.backgroundBox, this.colorBackground);
            theSpriteBatch.Draw(this.spriteTextureBackground2, this.backgroundBox2, Color.FromNonPremultiplied((255 - this.colorBackground.R), (255 - this.colorBackground.G), (255 - this.colorBackground.B), (255 - this.colorBackground.A)));
            theSpriteBatch.Draw(spriteTextureMoon, this.moonBox, this.colorBackground);
            theSpriteBatch.Draw(this.moonCircle, this.circlePosition, Color.White);
            theSpriteBatch.Draw(spriteTextureRyan, this.ryanBox, new Rectangle((this.ryanFrameColone - 1) * 60, (this.ryanFrameLigne - 1) * 60, 60, 60), Color.Black, 0f, Vector2.Zero, this.effetMirroir, 0f);

            if (this.waitJAMK > 500f)
            {
                theSpriteBatch.Draw(this.spriteTextureJAMK, this.jamkBox, Color.White);
            }

            if (this.waitJAMK > 1000f)
            {
                this.gameText.Draw(theSpriteBatch, this.fontImpact);
                this.presentText.Draw(theSpriteBatch, this.fontImpact);
            }
        }
    }
}
